# -*- coding: utf-8 -*-
'''
Created on Dec 19, 2013

@author: LONG HOANG GIANG
'''
import os, sys
sys.path.append(os.path.join(os.path.dirname(__file__), '../'))
import lib
import re

def getCategories():
    
    url = 'http://www.englishspeak.com/vi/english-phrases.cfm'
    tree = lib.Web.load(url).build_tree()
    data = []
    for node in tree.xpath("//select[@class='leftentry' and @name='categoryID']/option[position()>1]"):
        catName = lib.stringify(node)
        catId = node.get('value')
        data.append({'name': catName, 'id': catId})
    return data
    

def get1000MostCommonPhrases(catId):
    
    url = 'http://www.englishspeak.com/vi/english-phrases.cfm?newCategoryShowed={_id}&sortBy=28&displayEnglish=0'.format(_id=catId)
    
    tree = lib.Web.load(url).build_tree()
    viList = []
    speakList = []
    for trNode in tree.xpath("//table[@class='blacktext']//table/tr[position()>1]"):
        text = lib.stringify(trNode.xpath("./td[1]"))
        print text
        viList.append(text)
        speakerImg = trNode.xpath("./td[3]//img")[0]
        href = speakerImg.get('onclick')
        href = lib.extractText(r"playMp3\('(.+)'\)", href, 1)
        speakList.append(href)
        print href
        
    url = 'http://www.englishspeak.com/english-phrases.cfm?newCategoryShowed={_id}&sortBy=28'.format(_id=catId)
    tree = lib.Web.load(url).build_tree()
    enList = []
    for trNode in tree.xpath("//table[@class='blacktext']//table/tr[position()>1]"):
        text = lib.html2text(trNode.xpath("./td[1]"))
        print text
        enList.append(text)
    print len(viList), len(enList)
    
    
if __name__ == '__main__':
    
#     getCategories()
    get1000MostCommonPhrases(17)
    
    
    print '> Finished'
    sys.exit(0)